using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFileHistory
{
    public partial class FrmDrives : Form
    {
        public System.IO.DriveInfo SelectDrive { get; set; }

        public FrmDrives()
        {
            InitializeComponent();
        }

        private void FrmDrives_Load(object sender, EventArgs e)
        {
            this.localInitLayout();
        }

        private void localInitLayout()
        {
            var drives = System.IO.DriveInfo.GetDrives();
            lstDrives.Items.Clear();
            foreach (var drive in drives)
            {
                ListViewItem lv = new ListViewItem();
                lv.Tag = drive;
                if (drive.DriveType == System.IO.DriveType.Network) { lv.Text = ClsExts.ResolveToUNC(drive.Name); }
                else { lv.Text = drive.DriveType + " " + drive.Name; }
                lv.SubItems.Add(drive.AvailableFreeSpace.ToGB());
                lv.SubItems.Add(drive.TotalSize.ToGB());
                if (drive == SelectDrive) { lv.Selected = true; }
                lstDrives.Items.Add(lv);
            }
        }

        private void lstDrives_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDrives.SelectedItems.Count > 0)
            {
                this.SelectDrive = (System.IO.DriveInfo)lstDrives.SelectedItems[0].Tag;
            }
        }

        System.Diagnostics.Process _p_net_driver;
        bool _flag_keep_net_driver = false;
        char _temp_logic_disk = '\0';
        private void lbtnAddNetworkPath_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FolderBrowserDialog diag = new FolderBrowserDialog();
            //diag.RootFolder = Environment.SpecialFolder.Network;
            diag.Description = "选择一个网络位置，并映射到本地磁盘";
            diag.SelectedPath = "::\\";
            if (diag.ShowDialog() == DialogResult.OK)
            {
                this.release_net_driver();

                string sharePath = diag.SelectedPath;
                string cmd = System.IO.Path.Combine(Environment.SystemDirectory, "cmd.exe");
                //string par = "/s /k pushd \"" + sharePath + "\"";

                _temp_logic_disk = ClsExts.GetNextLogicalDisk();
                string par = "/s /k net use " + _temp_logic_disk + ": \"" + sharePath + "\"";

                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(cmd, par);
                psi.UseShellExecute = false;
                psi.CreateNoWindow = true;
                psi.RedirectStandardInput = true;
                _p_net_driver = System.Diagnostics.Process.Start(psi);
                _p_net_driver.WaitForExit(500);
                this.localInitLayout();
                //_p_net_driver.StandardInput.WriteLine("POPD");
                //_p_net_driver.StandardInput.WriteLine("net use " + _temp_logic_disk + ": /delete");
                //_p_net_driver.WaitForExit(500);
                //_p_net_driver.Kill();

            }
        }

        private void release_net_driver()
        {
            if (_p_net_driver != null)
            {
                //_p_net_driver.StandardInput.WriteLine("POPD");
                if (!_flag_keep_net_driver)
                {
                    _p_net_driver.StandardInput.WriteLine("net use " + _temp_logic_disk + ": /delete");
                    _p_net_driver.WaitForExit(500);
                }
                _p_net_driver.Kill();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            _flag_keep_net_driver = true;
        }

        private void FrmDrives_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.release_net_driver();
        }

    }
}
